package com.efruit.shopcart;

import java.util.Collection;

public class ShopCartProxy{
	private IProductInfoFetcher productInfoFetcher;
	private ShoppingList shoppingList;
	private float total;

	public ShopCartProxy() {
		shoppingList = new ShoppingList();
		total = 0;
	}

	public int addProduct(int id) {
		if(productInfoFetcher == null)
			productInfoFetcher = ShopCartFactory.getInstance().getProductInfoFetcher();
		IProductInfo productInfo = productInfoFetcher.getProductInfo(id);
		if (productInfo == null)
			return 0;
		ShoppingItem item = shoppingList.addProduct(productInfo);
		total += item.getPrice();
		return item.getAmount();
	}

	public void removeProduct(int id) {
		ShoppingItem item = shoppingList.removeProduct(id);
		total -= item.getPrice() * item.getAmount();
	}

	public boolean setProductAmount(int id, int amount) {
		ShoppingItem oldItem = shoppingList.setProductAmount(id, amount);
		if(oldItem == null)
			return false;
		total += oldItem.getPrice() * (amount - oldItem.getAmount());
		return true;
	}

	public Collection<ShoppingItem> getShoppingInfo() {
		return shoppingList.getShoppingInfo();
	}

	public float getTotal() {
		return total;
	}

}
